var vue = new Vue({
    el: "#app",
    data: {
        studentList: [],
        isFirstPage: false,
        pageNums: [],
        pageNum: 0,
        pageSize: 0,
        isLastPage: false,
        nextPage: 0,
        previousPage: 0,
    },
    methods: {
// :href="'/student/findByPage?pageNum='+pageNum+'&pageSize='+pageSize"
        findByPage: function (pageNum, pageSize) {
            let _this = this;
            axios.get("/student/findByPage?pageNum=" + pageNum + "&pageSize=" + pageSize).then(function (response) {
                // _this.pageInfo = response.data;
                let pageInfo = response.data;
                _this.studentList = pageInfo.list;
                _this.isFirstPage = pageInfo.isFirstPage;
                let pageNums = pageInfo.pages;
                _this.pageNum = pageInfo.pageNum;
                _this.isLastPage = pageInfo.isLastPage;
                _this.pageSize = pageInfo.pageSize;
                _this.nextPage = pageInfo.nextPage;
                _this.previousPage = pageNum - 1;
                //计算开始页与结束页
                let begin = pageNum - 5;
                let end = pageNum + 4;
                //小于10页则最后填满10页
                if (end <= 10) {
                    end = 10;
                }
                //大于总页数则将总数赋给最后一页
                if (end > pageNums) {
                    end = pageNums;
                    //开始页为结束页-9
                    begin = end - 9;
                }
                //开始页小于1 ，赋值1
                if (begin < 1) {
                    begin = 1;
                }
                //分页按钮数值 数组生成
                _this.pageNums = [];
                for (let i = begin; i <= end; i++) {
                    _this.pageNums.push(i);
                }

            }).catch(function (err) {
                console.log(err);
            });
        },
        //删除选中
        del:function () {
            let _this = this;
            if(confirm("确定要删除吗？")){
                axios.post("/student/del",$("#form").serialize()).then(function (response) {
                    alert("成功删除"+response.data+"条记录！");
                    _this.findByPage(_this.pageNum, _this.pageSize);
                })
            }
        },
    },
    created: function () {
        this.findByPage(1, 5);
    },
    updated: function () {
        let _this = this;
        //分页按钮 当前页显示 active样式
        $("#page li").each(function () {
            this.className = "";
            if (this.innerText == _this.pageNum) {
                this.className = 'active';
            }
        });
        // 隐藏下一页 和上一页按钮 方法
        function btn_hide() {
            _this.isFirstPage ?  $("#previous_btn").hide() :  $("#previous_btn").show();
            _this.isLastPage ?  $("#next_btn").hide() :  $("#next_btn").show();
        }
        //数据更新后 初始化
        btn_hide();

        //每条显示记录数 改变
        $("#pageSize").change(function () {
            _this.findByPage(1, $(this).val());
        });
        
    }

});